package hc;

import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import com.hazelcast.spi.properties.ClusterProperty;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MapGet {

    private PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    private HazelcastInstance hz;

    public MapGet(String publicAddress, int port, String member) {
        Config config = new Config();
        config.getNetworkConfig().setPublicAddress(publicAddress).setPort(port);
        config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true)
                .addMember(member);
        config.setProperty(ClusterProperty.EVENT_THREAD_COUNT.getName(), "10");

        hz = Hazelcast.newHazelcastInstance(config);
    }

    public void start() {
        performanceAnalyze.start();

        IMap<Integer, Long> map = hz.getMap("map");

        ExecutorService executorService = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 4; i++) {
            executorService.submit(() -> {
                int times = 100000000;
                for (int j = 0; j < times; j++) {
                    long start = System.currentTimeMillis();

                    Long time = map.get(9);

                    long end = System.currentTimeMillis();

                    performanceAnalyze.cpCounter();
                    performanceAnalyze.timeDelay(start - time);

                    times--;
                }
            });
        }

    }

}
